package org.example.two;

import org.example.two.myenum.AlgorithmType;
import org.example.two.sort.*;

import org.springframework.util.StopWatch;

public class SortCompare {
    public static double time(String alg, Double[] a) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        // 根据传入的算法名称，使用相应的排序算法对数组进行排序
        if (alg.equals(AlgorithmType.INSERTION.getValue())) {
            new Insertion().sort(a);
        } else if (alg.equals(AlgorithmType.SELECTION.getValue())) {
            new Selection().sort(a);
        } else if (alg.equals(AlgorithmType.SHELL.getValue())) {
            new Shell().sort(a);
        } else if (alg.equals(AlgorithmType.MERGEAMEND.getValue())) {
            new MerageAmend().sort(a);
        } else if (alg.equals(AlgorithmType.MERGE.getValue())) {
            new Merge().sort(a);
        } else if (alg.equals(AlgorithmType.MERGEBU.getValue())) {
            new MergeBU().sort(a);
        } else if (alg.equals(AlgorithmType.QUICK.getValue())) {
            new Quick().sort(a);
        } else if (alg.equals(AlgorithmType.QUICKAMEND01.getValue())) {
            new QuickAmend01().sort(a);
        } else if (alg.equals(AlgorithmType.QUICK3WAY.getValue())) {
            new Quick3way().sort(a);
        }else if (alg.equals(AlgorithmType.PILE.getValue())) {
            new Pile().sort(a);
        }

        stopWatch.stop();
        // 返回最后任务的运行时间（毫秒）
        return stopWatch.getLastTaskTimeMillis();
    }

    public static double timeRandomInput(String alg, int N, int T) {
        double total = 0.0;
        Double[] doubles = new Double[N];
        for (int i = 0; i < T; i++) {
            for (int j = 0; j < N; j++) {
                doubles[j] = Math.random();
                //doubles[j] = ((int)(Math.random()*50))/50.0;
            }
            total += time(alg, doubles);
        }
        return total;
    }


    public static void start(String[] arg) {
        String alg1 = arg[0];
        String alg2 = arg[1];
        int N = Integer.parseInt(arg[2]);
        int T = Integer.parseInt(arg[3]);
        double t1 = timeRandomInput(alg1, N, T);
        double t2 = timeRandomInput(alg2, N, T);
        System.out.println("For " + N + " random Double \n" +
                alg1 + " is " + t2 / t1 + " times faster then " + alg2);
        System.out.println(alg1 + ":" + t1 / T);
        System.out.println(alg2 + ":" + t2 / T);
    }

    public static void main(String[] args) {
        start(new String[]{AlgorithmType.QUICK.getValue(), AlgorithmType.PILE.getValue(), "100000", "50"});
    }
}
